package com.yinhai.message.implement.wx.adapter;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.util.MessageType;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.adapterManager.IAdapter;
import com.yinhai.message.core.event.IMessageEvent;
import com.yinhai.message.core.exception.ResultsHandle;
import com.yinhai.message.implement.wx.event.WechatMessageEvent;
import com.yinhai.message.implement.wx.sender.WechatMessageSender;

/**
 * 
 * @author yanglq
 *	2017.4.6
 */
@Component("wechatAdapter")
public class WechatAdapter implements IAdapter, ApplicationListener<WechatMessageEvent>{

    private static final Logger LOGGER = LoggerFactory.getLogger(WechatAdapter.class);

	@Resource(name = "resultsHandle")
	private ResultsHandle resultsHandle;

	@Resource(name = "wechatMessageSender")
	private WechatMessageSender wechatMessageSender;
	
	@Override
	public String getSenderType() {
		return MessageType.WECHAT;
	}

	@Override
	public IMessageEvent getEven(TMMessageVo vo) {
		return new WechatMessageEvent(vo);
	}
	
	@Async
	@Override
	public void onApplicationEvent(WechatMessageEvent event) {
		 LOGGER.debug("wechat处理事件===========================");
		 TMMessageVo messageSendVO = event.getMessageVo();
		 String mgid = messageSendVO.getMessage().getMgid();
		 List<TMUser> resuleUserList = wechatMessageSender.sendMessage(messageSendVO);
		 resultsHandle.handleResults(resuleUserList, mgid, getSenderType());
	}
}
